package com.example.springstydy.Service.impl;

import com.example.springstydy.Mapper.UserMapper;
import com.example.springstydy.Service.UserService;
import com.example.springstydy.pojo.User;
import com.example.springstydy.util.Md5Util;
import com.example.springstydy.util.ThreadLocalUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByName(String username) {
        User u=userMapper.findByName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        System.out.println(username+" , "+password);
        //加密
        if (password==null){
            System.out.println("错误！");
        }else{
            String md5String = Md5Util.getMD5String(password);
            //创建账户
            userMapper.add(username,md5String);
        }

    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer id=(Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }
}
